//
//  DataCell.swift
//  JKTools
//
//  Created by 姜奎 on 2/19/25.
//

import Cocoa

class SessionListItemCell: NSTableCellView {

    static let reuseIdentifier = NSUserInterfaceItemIdentifier("SessionListItemCell")

    private let idLabel: NSTextField = {
        let label = NSTextField(labelWithString: "")
        label.font = .systemFont(ofSize: 14, weight: .medium)
        label.textColor = .labelColor
        label.lineBreakMode = .byTruncatingTail
        label.isEditable = false
        label.isBordered = false
        return label
    }()

    private let timeLabel: NSTextField = {
        let label = NSTextField(labelWithString: "")
        label.font = .systemFont(ofSize: 12)
        label.textColor = .secondaryLabelColor
        return label
    }()

    private var model: SessionEntity? = nil

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        let stackView = NSStackView(views: [idLabel, timeLabel])
        stackView.orientation = .vertical
        stackView.alignment = .leading
        stackView.spacing = 4

        addSubview(stackView)

            // 自动布局约束
        stackView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
            stackView.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16)
        ])
    }

    func configure(with session: SessionEntity) {
        model = session
        idLabel.stringValue = session.title
        timeLabel.stringValue = session.timestamp.formattedTodayTime
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        idLabel.stringValue = ""
        timeLabel.stringValue = ""
    }
}

extension SessionListItemCell {

    override func rightMouseDown(with event: NSEvent) {
        let menu = NSMenu.menu()
        menu.addItem(title: "删除") { [weak self] in
            guard let model = self?.model else { return }
            NotificationCenter.default.post(
                name: .delSession,
                object: nil,
                userInfo: ["session": model]
            )
        }
        NSMenu.popUpContextMenu(menu, with: event, for: self)
    }
}

extension SessionListItemCell: CellProtocol {

    static func createOrUpdate<T, D, V>(_ contentView: T, with data: D) -> V? {

        guard let contentView = contentView as? NSOutlineView, let data = data as? SessionEntity else { return nil }

        if let cell = contentView.makeView(withIdentifier: SessionListItemCell.reuseIdentifier, owner: nil) as? SessionListItemCell {

            cell.configure(with: data)

            return cell as? V

        }

            // 创建新实例

        let newCell = SessionListItemCell()

        newCell.identifier = SessionListItemCell.reuseIdentifier // 关键：设置复用标识符

        newCell.configure(with: data)

        return newCell as? V
    }
}

extension Notification.Name {
    static let delSession = Notification.Name("delAiSessionNotification")
}
